#include "threadPool.h"
#include "strTok.h"
//字符串分割
int strToken(const char *command, userInfo_t *userInfo) {
    if (command == NULL || userInfo == NULL) {
        return -1; // 检查空指针
    }

    DEBUG_PRINT("in strToken\n");
    char commandCopy[4096];
    bzero(commandCopy, sizeof(commandCopy));
    strncpy(commandCopy, command, sizeof(commandCopy) - 1); // 安全拷贝

    // 分割名字
    char *token = strtok(commandCopy, "!");
    if (token == NULL) {
        DEBUG_PRINT("strToken failed: no userName\n");
        return -1;
    }
    strncpy(userInfo->userName, token, sizeof(userInfo->userName) - 1);
    DEBUG_PRINT("userInfo.userName = %s\n",userInfo->userName);
    // 分割密码
    token = strtok(NULL, "!");
    if (token == NULL) {
        DEBUG_PRINT("strToken failed: no userPassword\n");
        return -1;
    }
    strncpy(userInfo->userPassword, token, sizeof(userInfo->userPassword) - 1);
    DEBUG_PRINT("userInfo.userPassword = %s\n",userInfo->userPassword);

    // 分割 ciphertext（可能不存在）
    token = strtok(NULL, "!");
    if (token == NULL) {
        DEBUG_PRINT("no ciphertext, only name and password\n");
        return 0; // 可以返回 0，表示只有用户名和密码
    }
    strncpy(userInfo->ciphertext, token, sizeof(userInfo->ciphertext) - 1);
    DEBUG_PRINT("userInfo.ciphertext = %s\n",userInfo->ciphertext);

    // 分割命令（可能不存在）
    token = strtok(NULL, "!");
    if (token == NULL) {
        DEBUG_PRINT("no command\n");
        return 0; // 可以返回 0，表示没有命令
    }
    strncpy(userInfo->commamd, token, sizeof(userInfo->commamd) - 1);
    DEBUG_PRINT("userInfo.command = %s\n",userInfo->commamd);

    return 0;
}

////测试
//int main(){
//    char commamd[] = "lcw!lyy";
//    userInfo_t userInfo;
//    bzero(&userInfo,sizeof(userInfo));
//    strToken(commamd,&userInfo);
//    return 0;
//}
